<template>
  <main-page>
    <div slot="body">
      <div style="margin-bottom: 10px">
        <el-alert title="说明" show-icon type="info" :closable="false">
          <dl>
            <dd>
              1. 根据移动端界面， 分类需要分为两层
            </dd>
          </dl>
        </el-alert>
      </div>
      <el-form :inline="true" :model="search" size="mini">
        <el-form-item>
          <el-select
            v-model="search.blockId"
            style="width:100%"
            placeholder="类型"
            clearable
            @change="change2BlockId"
          >
            <el-option :key="config.blockType.man" label="男生" :value="config.blockType.man"></el-option>
            <el-option :key="config.blockType.woman" label="女生" :value="config.blockType.woman"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select v-model="search.parentId" placeholder="请选择" clearable style="width: 100%">
            <el-option
              v-for="sort in parentSortList"
              :key="sort.id"
              :label="sort.sortName"
              :value="sort.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getSortList">搜索</el-button>
          <el-button type="danger" @click="dialogFormVisible = true;">新增</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" style="width: 100%" tooltip-effect="dark">
        <el-table-column prop="id" label="编号" width="80"></el-table-column>
        <el-table-column label="类型">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.blockId == config.blockType.man">男生</el-tag>
            <el-tag v-if="scope.row.blockId == config.blockType.woman">女生</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="父节点">
          <template
            slot-scope="scope"
          >{{scope.row.parentVo == undefined ? "父节点" : scope.row.parentVo.sortName}}</template>
        </el-table-column>
        <el-table-column prop="sortName" label="类型名称"></el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">{{scope.row.createTime | datetime}}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              title="编辑"
              size="small"
              @click="handleClick2EditById(scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              title="删除"
              size="small"
              @click="handleClick2DeleteById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination :listNum="listNum" @currentChange="handleCurrentChange"></pagination>

      <el-dialog title="分类管理" :visible.sync="dialogFormVisible" width="40%">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="模块分类" prop="blockId">
            <el-radio-group v-model="form.blockId" @change="change2BlockId">
              <el-radio :label="config.blockType.man">男生</el-radio>
              <el-radio :label="config.blockType.woman">女生</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="父节点">
            <el-select v-model="form.parentId" placeholder="请选择" style="width: 100%">
              <el-option
                :key="0"
                label="父节点"
                :value="0"
              ></el-option>
              <el-option
                v-for="sort in parentSortList"
                :key="sort.id"
                :label="sort.sortName"
                :value="sort.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="分类名称" prop="sortName">
            <el-input v-model="form.sortName" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="onSubmit">提交</el-button>
        </div>
      </el-dialog>
    </div>
  </main-page>
</template>

<script>
import {
  getSortList,
  getSortAllList,
  saveSort,
  deleteSort,
  updateSortById
} from "api/SortApi";

import { config } from "assets/js/Data";

export default {
  name: "SortList",
  data() {
    return {
      dialogFormVisible: false,
      tableData: [],
      parentSortList: [],
      listNum: 0,
      search: {
        currentIndex: 1,
        blockId: "",
        parentId: ""
      },
      form: {
        id: "",
        blockId: "",
        parentId: 0,
        sortName: ""
      },
      rules: {
        blockId: [{ required: true, message: "请选择分类类型" }],
        parentId: [{ required: true, message: "请选择父节点" }],
        sortName: [{ required: true, message: "请输入分类名称" }]
      },
      config: config
    };
  },
  methods: {
    getSortList() {
      getSortList(this.search).then(res => {
        this.tableData = res.datas;
        this.listNum = res.pagination.totalCount;
      });
    },
    getSortParentList(blockId) {
      getSortAllList({
        parentId: 0,
        blockId
      }).then(res => {
        if (res) {
          this.parentSortList = res;
        }
      });
    },
    handleCurrentChange(page) {
      this.search.currentIndex = page;
      this.getSortList();
    },
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.form.id) {
            //修改
            updateSortById(this.form).then(res => {
              if (res) {
                this.$base.showOk();
                this.dialogFormVisible = false;
                this.getSortList();
              }
            });
          } else {
            //保存
            saveSort(this.form).then(res => {
              if (res) {
                this.$base.showOk();
                this.dialogFormVisible = false;
                this.getSortList();
              }
            });
          }
        }
      });
    },
    handleClick2EditById(row) {
      this.dialogFormVisible = true;
      this.$base.set2Form(this.form, row);
      this.getSortParentList(this.form.blockId);
    },
    handleClick2DeleteById(id) {
      this.$base.deleteConfirm(() => {
        deleteSort({ id }).then(res => {
          if (res) {
            this.$base.deleteOk();
            this.getSortList();
          }
        });
      });
    },
    change2BlockId(label) {
      this.getSortParentList(label);
    }
  },
  created() {
    this.getSortList();
    this.getSortParentList("");
  },
  watch: {
    dialogFormVisible(newDialogFormVisible) {
      if (!newDialogFormVisible) {
        this.$base.clearObj(this.form);
        this.getSortParentList("");
      }
    }
  }
};
</script>

<style lang="stylus" scoped></style>